#include <cstdio>

int main( )
{
    int n, m, i;
    __int64 ans;
    while ( scanf("%d%d", &n, &m) != EOF )
    {
        if ( n == -1 && m == -1 )
            break;
        if ( m == 1 )
        {
            puts("1");
            continue;
        }
        if ( m == 2 )
        {
            ans = 1;
            for ( i = 0; i < n; i++ )
                ans = ans * 2 % 987654321;
            printf("%I64d\n", ans);
            continue;
        }
        if ( n < m )
        {
            ans = 1;
            for ( i = 0; i < n; i++ )
                ans = ans * m % 987654321;
            printf("%I64d\n", ans);
            continue;
        }
        ans = 1;
        for ( i = 1; i <= m; i++ )
            ans = ans * i % 987654321;
        printf("%I64d\n", ( ans + m ) % 987654321);
    }
    return 0;
}
